/**
 * Copyright (c) 2009-2011, chunquedong(YangJiandong)
 * 
 * This file is part of ChunMap project
 * Licensed under the GNU LESSER GENERAL PUBLIC LICENSE(Version >=3)
 * 
 * History:
 *     2010-05-05  Jed Young  Creation
 */
package chunmap.model.crs.transf;

import chunmap.model.coord.Coordinate2D;
import chunmap.model.coord.Position;
import chunmap.model.coord.Transform;
import static java.lang.Math.*;

/**
 * 球面坐标
 * 
 * @author chunquedong
 * 
 */
public class SphericalPolar implements Transform {
	
	private double f0;
	private double l0;

	public SphericalPolar(double f0, double l0) {
		super();
		this.f0 = f0;
		this.l0 = l0;
	}

	@Override
	public Position convert(Position p) {
		double f=toRadians( p.getX());
		double l=toRadians( p.getY());
		
		double z=acos(cosz(f,l));
		double tana=sinzsina(f,l)/sinzcosa(f,l);
		double a=atan(tana);
		
		return new Coordinate2D(toDegrees(z),toDegrees(a));
	}
	
	protected double cosz(double f,double l){
		double cosz=sin(f)*sin(f0)+cos(f)*cos(f0)*cos(l-l0);
		return cosz;
	}
	
	protected double sinzcosa(double f,double l){
		double r=sin(f)*cos(f0)-cos(f)*sin(f0)*cos(l-l0);
		return r;
	}
	
	protected double sinzsina(double f,double l){
		return cos(f)*sin(l-l0);
	}

}